package com.gcsoftware.web.controller;

import com.gcsoftware.web.mapper.db.LoginMapper;
import com.gcsoftware.web.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import static com.gcsoftware.configs.AppConstants.Sys.SYS_COERCE_LOGOUT;
import static com.gcsoftware.configs.AppConstants.Sys.SYS_LOGOUT;

/**
 * 登陆
 * @author czp
 * date 2018-06-06
 */
@Controller
@RequestMapping("login")
public class LoginController extends BaseController {
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private LoginService loginService;

    @RequestMapping("")
    public String toLogin() {
        logger.info("toLogin()");
        return "login/login";
    }

    @RequestMapping("/toInitWizard")
    public String toInitWizard() {
        logger.info("toInitWizard()");
        return "login/initWizard";
    }

    @RequestMapping("/toUpPwd")
    public String toUpPwd() {
        logger.info("toUpPwd()");
        return "login/upPwd";
    }

    /**
     * 登录
     * @return 登录信息
     * curl http://localhost:8080/login/loginStatus -X POST -d "user=xx&pwd=xx"
     */
    @ResponseBody
    @RequestMapping(value = "loginStatus", method = RequestMethod.POST)
    public String loginStatus(@RequestParam("user") String name, @RequestParam("pwd") String pwd) {
        return loginService.loginStatus(name, pwd, request);
    }

    /**
     * 登出
     * @return 登出
     * curl http://localhost:8080/login/loginOut -X POST -d "state=xx"
     */
    @ResponseBody
    @RequestMapping(value = "loginOut", method = RequestMethod.POST)
    public boolean loginOut(@RequestParam("state") String state) {
        state = state.trim().equals(SYS_LOGOUT.getValue()) == false ? SYS_COERCE_LOGOUT.getName() : SYS_LOGOUT.getName();
        return loginService.loginOut(request, state);
    }

    /**
     * 用户名校验
     * @return boolean
     * curl http://localhost:8080/login/userNameCheck -d "userName=xx"
     */
    @ResponseBody
    @RequestMapping("/userNameCheck")
    public boolean userNameCheck(@RequestParam("userName") String userName) {
        logger.info("userNameCheck()");
        return loginMapper.getUserName().equals(userName.trim()) == true ? true : false;
    }

    /**
     * 获取密码保护问题
     * @return String
     * curl http://localhost:8080/login/getPwdQuestions
     */
    @ResponseBody
    @RequestMapping("/getPwdQuestions")
    public String getPwdQuestions() {
        logger.info("getPwdQuestions()");
        return loginMapper.getPwdQuestion();
    }

    /**
     * 答案校验
     * @return boolean
     * curl http://localhost:8080/login/answerCheck -d "answer=xx"
     */
    @ResponseBody
    @RequestMapping("/answerCheck")
    public boolean answerCheck(@RequestParam("answer") String answer) {
        logger.info("answerCheck()");
        return loginMapper.getPwdAnswer().equals(answer.trim()) == true ? true : false;
    }

    /**
     * 密码相关信息修改
     * @param pwd 密码
     * @param questions 问题
     * @param answer 答案
     * @return boolean
     */
    @ResponseBody
    @RequestMapping(value = "/pushPwdInfo", method = RequestMethod.POST)
    public boolean pushPwdInfo(@RequestParam("pwd") String pwd,
                              @RequestParam("questions") String questions,
                              @RequestParam("answer") String answer) {
        logger.info("pwd:" + pwd + " questions:" + questions + " answer:" + answer);
//        loginMapper.updateUserInfo(new SysUser("INIT_ID", pwd, questions, answer));
        return true;
    }

    /**
     * 是否已经初始化系统
     * @return true / false
     * curl http://locslhost:8080/login/isInitSys -X POST
     */
    @ResponseBody
    @RequestMapping(value = "isInitSys", method = RequestMethod.POST)
    public boolean isInitSys() {
        return loginMapper.isInitSys() != 0 ? true : false;
    }
}
